package com.link.base.base.buryingpoint.buriedanalysis.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.core.basic.controller.BasicControllerException;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import com.link.base.base.buryingpoint.buriedanalysis.service.BuriedAnalysisReportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.buryingpoint.buriedanalysis.model.BuriedAnalysisReport;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 埋点数据分析报表-埋点数据分析报表适用于图表
 *
 * @author 闫有霞
 * @date 2019/05/06 15:52
 */
@Controller
@RequestMapping("/link/buriedAnalysisReport")
public class BuriedAnalysisReportController extends BasicController<BuriedAnalysisReport> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private BuriedAnalysisReportService buriedAnalysisReportService;

    @Override
    public BasicService<BuriedAnalysisReport> getBasicService() throws Exception {
        return buriedAnalysisReportService;
    }

    /**
     * 根据广告ID查询该广告流量数据（包括今日、昨日、汇总数据）
     *
     * @author: 闫有霞
     * @date: 2019/5/6
     * @param:
     **/
    @RequestMapping(value = "/queryFlowByAppId")
    @ResponseBody
    public Map<String, Object> queryFlowByAppId(@RequestBody BuriedAnalysisReport entity, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(5);
        try {
            // 广告ID
            String appId = entity.getAppId();
            if (StringUtils.isBlank(appId)) {
                throw new BasicControllerException("广告ID不能为空");
            }
            //今日统计 - 筛选今日流量数据（通用方法，处理图表空值）
            String today = DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
            String todayStartDate = today + " 00:00:00";
            String todayEndDate = today + " 23:59:59";
            BuriedAnalysisReport todayParam = new BuriedAnalysisReport();
            todayParam.setAppId(appId);
            todayParam.setStartDate(todayStartDate);
            todayParam.setEndDate(todayEndDate);
            List<BuriedAnalysisReport> todayAnalysis = buriedAnalysisReportService.queryByAppId(todayParam);
            //昨日统计-筛选昨日流量数据（通用方法，处理图表空值）
            String yesterday = DateUtil.dateToStr(DateUtil.addDayToDate(new Date(), -1), "yyyy-MM-dd");
            String yesterdayStartDate = yesterday + " 00:00:00";
            String yesterdayEndDate = yesterday + " 23:59:59";
            BuriedAnalysisReport yesterdayParam = new BuriedAnalysisReport();
            yesterdayParam.setAppId(appId);
            yesterdayParam.setStartDate(yesterdayStartDate);
            yesterdayParam.setEndDate(yesterdayEndDate);
            List<BuriedAnalysisReport> yesterdayAnalysis = buriedAnalysisReportService.queryByAppId(yesterdayParam);
            //汇总统计
            BuriedAnalysisReport allParam = new BuriedAnalysisReport();
            allParam.setAppId(appId);
            List<BuriedAnalysisReport> allAnalysis = buriedAnalysisReportService.queryByAppId(allParam);
            result.put("today", todayAnalysis.get(0));
            result.put("yesterday", yesterdayAnalysis.get(0));
            result.put("all", allAnalysis.get(0));
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据广告ID日期查询该广告流量数据
     *
     * @author: 闫有霞
     * @date: 2019/5/6
     * @param:
     **/
    @RequestMapping(value = "/queryByAppId")
    @ResponseBody
    public Map<String, Object> queryByAppId(@RequestBody BuriedAnalysisReport entity, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(2);
        try {
            List<BuriedAnalysisReport> list = new ArrayList<>();
            // 广告ID
            String appId = entity.getAppId();
            if (StringUtils.isBlank(appId)) {
                throw new BasicControllerException("广告ID不能为空");
            }
            String dateRange = entity.getDateRange();
            String groupByField = entity.getGroupByField();
            BuriedAnalysisReport param = new BuriedAnalysisReport();
            param.setAppId(appId);
            param.setGroupByField(groupByField);
            String today = DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
            if (StringUtils.equals(dateRange, "1")) {
                list = buriedAnalysisReportService.queryTodayByAppId(param);
            } else if (StringUtils.equals(dateRange, "7")) {
                String yesterday = DateUtil.dateToStr(DateUtil.addDayToDate(new Date(), -7), "yyyy-MM-dd");
                String yesterdayStartDate = yesterday + " 00:00:00";
                String yesterdayEndDate = today + " 23:59:59";
                param.setStartDate(yesterdayStartDate);
                param.setEndDate(yesterdayEndDate);
                list = buriedAnalysisReportService.queryByAppId(param);
            } else {
                list = buriedAnalysisReportService.queryByAppId(param);
            }
            result.put("result", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据广告ID查询转发分享数据
     *
     * @author: 闫有霞
     * @date: 2019/5/6
     * @param:
     **/
    @RequestMapping(value = "/queryShareByAppId")
    @ResponseBody
    public Map<String, Object> queryShareByAppId(@RequestBody BuriedAnalysisReport entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(2);
        try {
            // 广告ID
            String appId = entity.getAppId();
            if (StringUtils.isBlank(appId)) {
                throw new BasicControllerException("广告ID不能为空");
            }
            BuriedAnalysisReport param = new BuriedAnalysisReport();
            param.setAppId(appId);
            List<BuriedAnalysisReport> list = buriedAnalysisReportService.queryShareByAppId(param);
            result.put("result", list);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
